﻿using System.Configuration;

namespace Ayatta.Common.Configurations
{
    public class FtpConfigurationSetion : ConfigurationSection
    {
        private static ConfigurationPropertyCollection properties;

        private static bool readOnly;

        public FtpConfigurationSetion()
        {
            properties = new ConfigurationPropertyCollection
                             {
                                 new ConfigurationProperty("ip", typeof (string), "127.0.0.1",ConfigurationPropertyOptions.IsRequired),
                                 new ConfigurationProperty("username", typeof (string),string.Empty,ConfigurationPropertyOptions.IsRequired),
                                 new ConfigurationProperty("password", typeof (string), string.Empty,ConfigurationPropertyOptions.IsRequired),
                                 new ConfigurationProperty("server", typeof (string), string.Empty,ConfigurationPropertyOptions.IsRequired)
                             };
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get { return properties; }
        }

        private new  bool IsReadOnly
        {
            get { return readOnly; }
        }

        private void ThrowIfReadOnly(string propertyName)
        {
            if (IsReadOnly)
                throw new ConfigurationErrorsException("The property " + propertyName + " is read only.");
        }


        protected override object GetRuntimeObject()
        {
            readOnly = true;
            return base.GetRuntimeObject();
        }

        [StringValidator(MinLength = 1, MaxLength = 60)]
        public string Ip
        {
            get { return this["ip"] as string; }
            set
            {
                ThrowIfReadOnly("ip");
                this["ip"] = value;
            }
        }

        [StringValidator(MinLength = 1, MaxLength = 60)]
        public string UserName
        {
            get { return this["username"] as string; }
            set
            {
                ThrowIfReadOnly("username");
                this["username"] = value;
            }
        }

        [StringValidator(MinLength = 1, MaxLength = 60)]
        public string Password
        {
            get { return this["password"] as string; }
            set { this["password"] = value; }
        }

        [StringValidator(MinLength = 1, MaxLength = 60)]
        public string Server
        {
            get { return this["server"] as string; }
            set { this["server"] = value; }
        }
    }
}